package combat;

import java.util.Random;

import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageByEntityEvent;

import actives.ActiveManager;
import actives.ActiveType;
import actives.Active.ActiveState;

import skills.Skill;
import skills.SkillHandler;
import skills.Skills;

public class Swords {

	public static void swordCombat(EntityDamageByEntityEvent event) {
		Player damager = (Player) event.getDamager();
		if (damager.getItemInHand().getType().name().contains("_SWORD")) {
			LivingEntity target = (LivingEntity) event.getEntity();
			Skill swords = SkillHandler.getSkill(damager, Skills.SWORDS);
			double damage = event.getDamage();
			if (ActiveManager.getState(damager, ActiveType.SWORDS).equals(ActiveState.PRIMED)) {
				ActiveManager.activate(damager, ActiveType.SWORDS, null);
				double missingHealth = target.getMaxHealth() - target.getHealth();
				damage = Math.round((damage * swords.level() / 2000.0) + (damage * 0.05 * missingHealth));
			}
			else {
				damage = damage * (1 + swords.level() / 2000.0);
				if (swords.level() >= 250) {
					if (criticalHit(damager)) {
						damage = damage * 1.5;
						damager.sendMessage("Critical Strike!");
						if (swords.level() >= 750) {
							BleedManager.check(damager, target);
						}
					}
				}				
			}
			event.setDamage((int) damage);
			swords.addExp(event.getDamage());
		}
	}

	private static boolean criticalHit(Player player) {
		int level = SkillHandler.getSkill(player, Skills.SWORDS).level();
		double chance = level * .3 / 1000.0;
		double proc = new Random().nextDouble();
		return proc <= chance;
	}
}
